<!--
 * @Descripttion: 海报分类
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-19 17:48:37
-->
<template>
  <div class="lb-poster-classify">
    <top-nav />
    <div class="page-main">
      <div class="page-top-operate">
        <lb-button
          type="primary"
          size="medium"
          icon="el-icon-plus"
          @click="openDialog('add')"
          >添加海报分类</lb-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="加入时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column label="是否上架">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updatePosterClassifyInfo(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="
                  openDialog('edit', {
                    id: scope.row.id,
                    title: scope.row.title,
                    top: scope.row.top
                  })
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="delPosterClassify(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="page"
        :pageSize="limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
    <el-dialog
      :title="dialogForm.id ? '编辑分类' : '添加分类'"
      :visible.sync="centerDialogVisible"
      width="500px"
      center
    >
      <div class="dialog-inner">
        <el-form @submit.native.prevent label-width="80px" :model="dialogForm">
          <el-form-item label="分类名">
            <el-input
              v-model="dialogForm.title"
              placeholder="请输入分类名"
            ></el-input>
          </el-form-item>
          <el-form-item label="排序值">
            <el-input-number
              class="lb-input-number"
              v-model="dialogForm.top"
              :controls="false"
              :precision="0"
              :min="0"
              placeholder="请输入排序值"
            ></el-input-number>
            <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editPosterInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      dialogForm: {
        id: '',
        title: '',
        top: ''
      },
      centerDialogVisible: false,
      limit: 10,
      page: 1,
      total: 0
    }
  },
  created () {
    this.getPosterClassifyList()
  },
  methods: {
    /**
     * @method 获取海报分类列表数据
     */
    getPosterClassifyList () {
      this.loading = true
      let { limit, page } = this
      this.$api.getPosterClassifyList({ page_count: limit, page }).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    updatePosterClassifyInfo (id, status) {
      this.$api.updatePosterClassifyInfo({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
          }
          this.getPosterClassifyList()
        }
      })
    },
    /**
     * @method 打开对话框
     */
    openDialog (type, formObj) {
      if (type === 'edit') {
        this.dialogForm = formObj
      } else {
        this.dialogForm = {
          id: '',
          title: '',
          top: ''
        }
      }
      this.centerDialogVisible = true
    },
    /**
     * @method 编辑、添加海报分类
     */
    editPosterInfo () {
      let { dialogForm } = this
      if (!dialogForm.title) {
        this.$message.error('请输入分类名！')
      } else if (dialogForm.top === '' || !/^\d+$/.test(dialogForm.top)) {
        this.$message.error('请输入数字类型的排序值')
      } else {
        if (dialogForm.id) {
          this.$api.editPosterClassifyInfo(dialogForm).then(res => {
            if (res.code === 200) {
              this.tipsSuccess()
            }
          })
        } else {
          this.$api.addPosterClassifyInfo(dialogForm).then(res => {
            if (res.code === 200) {
              this.tipsSuccess()
            }
          })
        }
      }
    },
    tipsSuccess () {
      this.$message.success(this.$t('tips.successSub'))
      this.centerDialogVisible = false
      this.getPosterClassifyList()
    },
    /**
     * @method 删除分类
     */
    delPosterClassify (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updatePosterClassifyInfo(id, -1)
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getPosterClassifyList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-poster-classify {
  width: 100%;
  .page-main {
    width: 100%;
  }
  .dialog-inner {
    width: 100%;
    .el-input,
    .el-input-number {
      width: 300px;
    }
  }
}
</style>
